package com.example.boot_consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.TextMessage;

/**
 * boot 消费者-队列
 */
@Component
public class QueueConsumer {

    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    Queue queue;

    public void recieve() throws JMSException {
        System.out.println("****boot 1号消费者");
        Message msg = jmsMessagingTemplate.receive(queue);
        //生产者在发送的时候放到了payload里
        System.out.println("接收到的队列消息为：" + msg.getPayload());
    }

    //监听接收的方法
    @JmsListener(destination="${queue_name}")
    public void consumerMsg(TextMessage textMessage) throws JMSException {
         String text = textMessage.getText();
        System.out.println("***消费者收到的队列消息:    "+ text);
    }

}